package com.iot.platform.mvc.product.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iot.common.core.exception.ModelException;
import com.iot.common.core.util.AppUtil;
import com.iot.common.core.util.R;
import com.iot.common.core.util.UuidUtil;
import com.iot.common.security.annotation.SecurityAuthorization;
import com.iot.platform.api.domain.Product;
import com.iot.platform.api.domain.ProductCategory;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

/**
 * 日志表 前端控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/product")
@Api(value = "product", tags = "产品")
public class ProductController {

	@Setter
	private Throwable cause;

	@SecurityAuthorization
	@GetMapping(value = { "/list" }, produces = "application/json; charset=utf-8")
	public R<Page<Product>> list(@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {

		String sqlExceptSelect = " FROM " + Product.TABLE_NAME + " o  WHERE 1=1 ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " AND ( O.NAME LIKE '%" + keywords + "%' OR   O.KEY LIKE '%" + keywords + "%' ) ";
		}
		sqlExceptSelect += "  ORDER BY   O.UPDATE_TIME desc ";
		return R.ok(Product.dao.paginate(cur, limit, "select * ", sqlExceptSelect));
	}

	@SecurityAuthorization
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<Product> save(@Valid Product product) {
		if (product == null) {
			throw new ModelException("对象不可以为空！");
		}
		if (StrKit.isBlank(product.getId())) {
			product.setId(UuidUtil.getUUID());
			product.setKey(AppUtil.getAppId());
			product.setSecret(AppUtil.getAppSecret(product.getKey()));
			if (product.save()) {
				return R.ok();
			}
		} else {
			if (product.update()) {
				return R.ok();
			}
		}
		return R.failed();
	}

	@SecurityAuthorization
	@PostMapping(value = { "/deleteById" }, produces = "application/json; charset=utf-8")
	public R<Boolean> deleteById(@RequestParam(name = "id", required = true) String id) throws Exception {
		if (Product.dao.deleteById(id)) {
			return R.ok(true);
		} else {
			return R.failed(false);
		}
	}

	@SecurityAuthorization
	@GetMapping(value = { "/getInfo" }, produces = "application/json; charset=utf-8")
	public R<Product> getInfo(@RequestParam(name = "id", required = false, defaultValue = "") String id)
			throws Exception {
		Product product = Product.dao.findById(id);
		if (product == null) {
			throw new Exception("未查询到数据！");
		}
		product.setProductCategory(ProductCategory.dao.findById(product.getCategoryId()));
		return R.ok(product);
	}

	@SecurityAuthorization
	@GetMapping(value = { "/getSelectOptions" }, produces = "application/json; charset=utf-8")
	public R<List<Product>> getSelectOptions(
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords) {
		String sqlExceptSelect = "SELECT *  FROM " + Product.TABLE_NAME + " o  WHERE 1=1 ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " AND ( O.NAME LIKE '%" + keywords + "%' OR   O.CODE LIKE '%" + keywords + "%' ) ";
		}
		sqlExceptSelect += "  ORDER BY   O.UPDATE_TIME desc ";
		return R.ok(Product.dao.find(sqlExceptSelect));
	}
	
	@SecurityAuthorization
	@PostMapping(value = { "/resetSecret" }, produces = "application/json; charset=utf-8")
	public R<String> resetSecret(@RequestParam(name = "id", required = true) String id) throws Exception {
		Product product=Product.dao.findById(id);
		if(product==null) {
			return R.failed("数据不存在","");
		}
		String secret=AppUtil.getAppSecret(product.getKey());
		product.setSecret(secret);
		if (product.update()) {
			return R.ok(secret);
		} else {
			return R.failed("重置失败！","");
		}
	}
	
}
